// 通过对象的键对对象进行排序
export const SortObjectByKey = (obj: any, order: 'asc' | 'desc' = 'desc') => {
  return Object.fromEntries(
    Object.entries(obj).sort((a, b) => (order === 'asc' ? a[0].localeCompare(b[0]) : b[0].localeCompare(a[0])))
  )
}

// 通过指定的键对对象数组进行排序 未验证可行性
export const SortObjectArrayByKey = (objArray: any[], key: string, order: 'asc' | 'desc' = 'desc') => {
  return objArray.sort((a, b) => (order === 'asc' ? a[key].localeCompare(b[key]) : b[key].localeCompare(a[key])))
}

// 通过指定的键对对象数组进行排序 未验证可行性
export const SortObjectArrayByKeys = (objArray: any[], keys: string[], order: 'asc' | 'desc' = 'desc') => {
  return objArray.sort((a, b) => {
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i]
      if (a[key] < b[key]) {
        return order === 'asc' ? -1 : 1
      }
      if (a[key] > b[key]) {
        return order === 'asc' ? 1 : -1
      }
    }
    return 0
  })
}
